from spass.api.api_db import (get_users_list,
                              get_teams_list,
                              get_teamsets_list,
                              get_templates_list,
                              get_assignments_list)
from spass.api.api_warehouse import (wlist_assignment_versions,
                                     wlist_assignment_version_files,
                                     wlist_assignment_version_tests,
                                     wlist_assignment_tests)
from spass.core.error import error
from spass.core.message import message
from optparse import OptionParser

def callback_user(option, opt_str, value, parser): 
    users = get_users_list()
    if len(users) == 0:
        print 
    else:
        for user in users:
            message('user.identity', [str(user.id), user.fullname, user.name])
    return 0

def callback_team(option, opt_str, value, parser):
    teams = get_teams_list()
    if len(teams) == 0:
        message('not.any.teams', [])
    else:
        for team in teams:
            print "Id : %s" % team.id_team
    return 0

def callback_teamset(option, opt_str, value, parser):
    teamsets = get_teamsets_list()
    if len(teamsets) == 0:
        message('not.any.teamset', [])
    else:
        for teamset in teamsets:
            print "Id : %s" % teamset.id_team_set
    return 0

def callback_template(option, opt_str, value, parser):
    templates = get_templates_list()
    if len(templates) == 0:
        message('not.any.template', [])
    else:
        for template in templates:
            message('template.identity', [str(template.id), template.title])
    return 0

def callback_assignment(option, opt_str, value, parser):
    assignments = get_assignments_list()
    if len(assignments) == 0:
        message('not.any.assignment', [])
    else:
        for assignment in assignments:
            message('infos.details', [str(assignment.id), str(assignment.id_template), str(assignment.date), str(assignment.id_teamset), str(assignment.id_user)])
    return 0

def callback_assignment_versions(option, opt_str, value, parser):
    setattr(parser.values, option.dest, value)
    wlist_assignment_versions(parser.values.assignment[0], parser.values.assignment[1])
    return 0

def callback_assignment_version_files(option, opt_str, value, parser):
    setattr(parser.values, option.dest, value)
    wlist_assignment_version_files(parser.values.version[0], parser.values.version[1], parser.values.version[2])
    return 0

def callback_assignment_version_tests(option, opt_str, value, parser):
    setattr(parser.values, option.dest, value)
    wlist_assignment_version_tests(parser.values.version[0], parser.values.version[1], parser.values.version[2])
    return 0

def callback_assignment_tests(option, opt_str, value, parser):
    setattr(parser.values, option.dest, value)
    wlist_assignment_tests(parser.values.assignment)
    return 0

def list(argv):
    if len(argv) < 1:
        error('args.need', ['list', ''])
        return 1

    parser = OptionParser()
    parser.set_usage("""spass list <options...>""")
    parser.add_option("--user", action="callback", callback=callback_user,
                      help="Lists all users")
    parser.add_option("--team", action="callback", callback=callback_team,
                      help="Lists all teams")
    parser.add_option("--teamset", action="callback", callback=callback_teamset,
                      help="Lists all team sets")
    parser.add_option("--template", action="callback", callback=callback_template,
                      help="Lists all templates")
    parser.add_option("--assignment", action="callback",
                      callback=callback_assignment,
                      help="Lists all assignments")
    parser.add_option("--assignment-test", action="callback",
                      callback=callback_assignment_tests, type="string",
                      nargs=1, dest="assignment",
                      help="Lists all tests for an assignment")
    parser.add_option("--assignment-ver", action="callback",
                      callback=callback_assignment_versions, type="string",
                      nargs=2, dest="assignment",
                      help="Lists all versions for a team "\
                          "from an assignment. ARG : <assignment ID> <team ID>")
    parser.add_option("--assignment-ver-file", action="callback",
                      callback=callback_assignment_version_files, type="string",
                      nargs=3, dest="version", 
                      help="Lists all files from an assignment version."\
                          "ARG : <assignment ID> <team ID> <version ID>")
    parser.add_option("--assignment-ver-test", action="callback",
                      callback=callback_assignment_version_tests, type="string",
                      nargs=3, dest="version",
                      help="Lists all test logs from an assignment version. "\
                          "ARG : <assignment ID> <team ID> <version ID>")
    (options, args) = parser.parse_args(argv)
    return 0
